#!/bin/sh

#
# Test that creating a FAT filesystem produces the exact same image
# even when the time changes.
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
define(BOOT_PART_OFFSET, 63)
define(BOOT_PART_COUNT, 77238)

file-resource 1K.bin {
	host-path = "${TESTFILE_1K}"
}

mbr mbr-a {
    partition 0 {
        block-offset = \${BOOT_PART_OFFSET}
        block-count = \${BOOT_PART_COUNT}
        type = 0xc # FAT32
        boot = true
    }
}
task complete {
	on-init {
                mbr_write(mbr-a)
                fat_mkfs(\${BOOT_PART_OFFSET}, \${BOOT_PART_COUNT})
        }
        on-resource 1K.bin {
                fat_write(\${BOOT_PART_OFFSET}, "1K.bin")
        }
}
EOF

# Create the firmware file, then "burn it"
$FWUP_CREATE -c -f $CONFIG -o $FWFILE

export VERIFY_SYSCALLS_CHECKPATH=${IMGFILE}.1
$FWUP_APPLY -a -d ${IMGFILE}.1 -i $FWFILE -t complete
sleep 1

# This should be bit identical even though it is done later.
# By default, FAT stores timestamps. fwup is supposed to insure
# that they are identical between runs.
export VERIFY_SYSCALLS_CHECKPATH=${IMGFILE}.2
$FWUP_APPLY -a -d ${IMGFILE}.2 -i $FWFILE -t complete

diff ${IMGFILE}.1 ${IMGFILE}.2

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE
